package leetcode.code2165;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import leetcode.IDebug;

public class Solution implements IDebug {

	public long smallestNumber(long num) {
		List<Long> list = new ArrayList<>();
		boolean negative = num < 0 ? true : false;
		num = negative ? -num : num;
		while (num > 0) {
			list.add(num % 10);
			num /= 10;
		}
		Collections.sort(list);
		if (negative) {
			long ans = 0;
			for (int i = list.size() - 1; i >= 0; i--) {
				ans *= 10;
				ans += list.get(i);
			}
			return -ans;
		} else {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i) != 0) {
					if (i != 0) {
						long tmp = list.get(i);
						list.remove(i);
						list.add(0, tmp);
					}
					break;
				}
			}
			long ans = 0;
			for (int i = 0; i < list.size(); i++) {
				ans *= 10;
				ans += list.get(i);
			}
			return ans;
		}
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
